﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Drawing;

namespace SignalBox.Core.Model.Timetable
{
    public class TrainSchedule
    {

        private List<TrainScheduleAction> scheduleActions;

        public TrainSchedule()
        {
            scheduleActions = new List<TrainScheduleAction>();

            RunsFriday = true;
            RunsMonday = true;
            RunsThursday = true;
            RunsTuesday = true;
            RunsWednesday = true;
            RunsSunday = true;
            RunsSaturday = true;
        }

        public List<TrainScheduleAction> ScheduleActions
        {
            get { return scheduleActions; }
        }

        public Timetable Timetable
        {
            get;
            set;
        }
        public bool RunsMonday
        {
            get;
            set;
        }

        public bool RunsTuesday
        {
            get;
            set;
        }

        public bool RunsWednesday
        {
            get;
            set;
        }

        public bool RunsThursday
        {
            get;
            set;
        }
        public bool RunsFriday
        {
            get;
            set;
        }

        public bool RunsSaturday
        {
            get;
            set;
        }

        public bool RunsSunday
        {
            get;
            set;
        }

        public string TrainCode
        {
            get;
            set;
        }

        public TrainType TrainType
        {
            get;
            set;
        }

        public void SortSchedules()
        {
            ScheduleActions.Sort(delegate(TrainScheduleAction a, TrainScheduleAction b)
            {
                if (String.IsNullOrEmpty(a.Time)) return 1;
                if (String.IsNullOrEmpty(b.Time)) return 1;
                return a.Time.CompareTo(b.Time);
            });
        }

        public XElement ToXml()
        {

            return new XElement("TrainSchedule",
                new XElement("Code", TrainCode),
                new XElement("Monday", RunsMonday),
                new XElement("Tuesday", RunsTuesday),
                new XElement("Wednesday", RunsWednesday),
                new XElement("Thursday", RunsThursday),
                new XElement("Friday", RunsFriday),
                new XElement("Saturday", RunsSaturday),
                new XElement("Sunday", RunsSunday),
                new XElement("TrainType", TrainType.TypeCode),
                new XElement("TrainScheduleActions",
                    from TrainScheduleAction tsa in ScheduleActions
                    select tsa.ToXml()));

        }

        public void FromXml(XElement elem)
        {
            RunsMonday = bool.Parse(elem.Element("Monday").Value);
            RunsTuesday = bool.Parse(elem.Element("Tuesday").Value);
            RunsWednesday = bool.Parse(elem.Element("Wednesday").Value);
            RunsThursday = bool.Parse(elem.Element("Thursday").Value);
            RunsFriday = bool.Parse(elem.Element("Friday").Value);
            RunsSaturday = bool.Parse(elem.Element("Saturday").Value);
            RunsSunday = bool.Parse(elem.Element("Sunday").Value);
            TrainType = Timetable.FindTrainType(elem.Element("TrainType").Value);
            TrainCode = elem.Element("Code").Value;

            foreach (XElement action in elem.Element("TrainScheduleActions").Elements("ScheduleAction"))
            {
                string type = action.Element("Type").Value;
                if (type == "EnterLayout")
                {
                    EnterLayoutAction ela = new EnterLayoutAction();
                    ela.Schedule = this;
                    ela.Location = action.Element("Location").Value;
                    ela.Time = action.Element("Time").Value;
                    scheduleActions.Add(ela);
                }
                else if (type == "ExitLayout")
                {
                    ExitLayoutAction ela = new ExitLayoutAction();
                    ela.Schedule = this;
                    ela.Location = action.Element("Location").Value;
                    ela.Time = action.Element("Time").Value;
                    scheduleActions.Add(ela);
                }
                else if (type == "StopAtLocation")
                {
                    StopAtLocationAction sala = new StopAtLocationAction();
                    sala.Schedule = this;
                    sala.Location = action.Element("Location").Value;
                    sala.Time = action.Element("Time").Value;
                    sala.WaitTime = int.Parse(action.Element("WaitTime").Value);
                    sala.DepartureTime = action.Element("DepartureTime").Value;
                    scheduleActions.Add(sala);
                }
            }

            SortSchedules();
        }
        
    }
}
